package com.loong.musicdemo.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;

import java.util.List;

/**
 * 用于歌曲页面的ViewPager子项使用
 */
public class PageSongListAdapter extends RecyclerView.Adapter<PageSongListAdapter.ViewHolder> {
    private List<Song> songList;
    private int highLightPosition = -1;

    public PageSongListAdapter(List<Song> songList) {
        this.songList = songList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page_song_list, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Song song = songList.get(position);
        //设置高亮行
        try {
            if (!isSongLight(position)) {
                holder.songName.setTextColor(Color.parseColor("#000000"));//先设置普同行颜色,防止直接套用
                holder.authorName.setTextColor(Color.parseColor("#dddddd"));//先设置普同行颜色,防止直接套用
            } else {
                holder.songName.setTextColor(R.color.neteaseRed);
                holder.authorName.setTextColor(R.color.neteaseRed);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.songName.setText(song.getName());
        holder.songName.setOnClickListener(v -> {
            if (onClickSongItem != null) {
                onClickSongItem.onSongClick(position);
            }
        });//播放当前音乐
        holder.authorName.setText(song.getSingerName());
        holder.deleteCurrent.setOnClickListener(v -> {
            if (onClickSongItem != null) {
                onClickSongItem.onDeleteClick(position);
            }
        });//删除当前歌曲

    }

    public interface OnClickSongItem {
        void onDeleteClick(int position);//删除按钮

        void onSongClick(int position);//播歌点击
    }

    private OnClickSongItem onClickSongItem;

    public void setOnClickSongItem(OnClickSongItem onClickSongItem) {
        this.onClickSongItem = onClickSongItem;
    }

    @Override
    public int getItemCount() {
        return songList == null ? 0 : songList.size();
    }

    /**
     * 设置高亮行
     *
     * @param position 高亮行位置
     */
    public void clickedItem(int position) {//高亮显示设置
        notifyItemChanged(highLightPosition);//同步之前高亮行
        highLightPosition = position;
        notifyItemChanged(position);
    }

    /**
     * 判断是否为高两行
     *
     * @param position 当前行位置
     * @return 是否高亮
     */
    public boolean isSongLight(int position) {
        return highLightPosition == position;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout root;
        TextView songName;//歌曲名
        TextView authorName;//歌手名
        ImageButton deleteCurrent;//删除当前

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            songName = itemView.findViewById(R.id.song_name);
            authorName = itemView.findViewById(R.id.author_name);
            deleteCurrent = itemView.findViewById(R.id.delete_current);

        }
    }
}
